#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2005;

int arr[N], dp[N][N][2], n, T, ans;

signed main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> T;
    while (T--)
    {
        memset(dp, 0, sizeof dp);
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> arr[i];
        for (int l = 2; l <= n; l++)
            for (int a = 0; a < l - 1; a++)
            {
                dp[l][a][0] = max(dp[l][a][0], dp[l - 1][a][0] + (arr[l - 1] == arr[l]) * arr[l]);
                dp[l][a][1] = max(dp[l][a][1], dp[l - 1][a][1] + (arr[l - 1] == arr[l]) * arr[l]);
                dp[l][l - 1][0] = max(dp[l][l - 1][0], dp[l - 1][a][1] + (arr[a] == arr[l]) * arr[l]);
                dp[l][l - 1][1] = max(dp[l][l - 1][1], dp[l - 1][a][0] + (arr[a] == arr[l]) * arr[l]);
            }
        ans = -1;
        for (int i = 1; i < n; i++)
            ans = max(ans, dp[n][i][0]), ans = max(ans, dp[n][i][1]);
        cout << ans << '\n';
    }
    return 0;
}
